package Linked_New;

public class Linked {
    public int a;
    public Linked next = null;
    public Linked temp,top;
    public int element;


    public Linked(int a) {
        this.a = a;
    }

    public String getnum(){
        String b = Integer.toString(a);
        return b;
    }

    public void setNext(Linked next){
        this.next=next;
    }

    public Linked getNext() {
        return next;
    }

    public void insert(Linked Head, int a, Linked insertnode){
        Linked temp = Head;
        if(a==0){
            insertnode.setNext(temp);
        }
        else {
            for(int i=0;i<a;i++)
            {
                top = temp;
                temp = temp.getNext();
            }
            top.setNext(insertnode);
            insertnode.setNext(temp);
            temp=top;
        }
    }

    public void delete(Linked Head, int a){
        Linked temp = Head;
        int i=0;
        if(a==0)
        {
            ;
        }
        else {
            while(i!=a-1){
                temp = temp.getNext();
                i++;
            }
            top = temp;
            temp=temp.getNext();
            temp=temp.getNext();
            top.setNext(temp);
            temp=top;
        }
    }

    public void sort(Linked Head, int count){
        Linked temp = Head;
        Linked c,d;
        c = temp;
        d = c.getNext();
        for(int i = 0;i<count-1;i++)
        {
            c = temp;
            d = c.getNext();
            for(int j =0;j<count-i-1;j++)
            {
                if(c.getElement()<d.getElement())
                {
                    int t;
                    t = c.getElement();
                    c.setElement(d.getElement());
                    d.setElement(t);
                }
                System.out.println(Head.print(Head));
                c=c.getNext();
                d=d.getNext();
            }
        }
    }

    public int getElement()
    {
        return a;
    }

    public void setElement (int elem)
    {
        a=elem;
    }


    public String print(Linked head){
        String result="Elements is:";
        for(;;)
        {
            if(head.getNext()==null){
                result +=head.getnum();
                break;
            }
            else
            {
                result += head.getnum()+",";
                head=head.getNext();
            }
        }
        return result;
    }

}
